﻿using UpperComputer.Base.Core.Entities;
using UpperComputer.Base.Core.Locators;
using YFurion.Application;
using YFurion.Extensions;

namespace UpperComputer.Base.SystemApplication
{
    public class DataDictionaryService : MSApplicationService<DataDictionary, DataDictionaryResultDto, long, DataDictionaryResultRequestDto, DataDictionaryDto, BaseMasterDbContextLocator, BaseSlaveDbContextLocator>, IDataDictionaryService
    {
        #region 获取树形数据字典
        /// <summary>
        /// 获取树形数据字典
        /// </summary>
        /// <param name="input"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<DataDictionaryResultDto>> GetDataDictionarys(DataDictionaryResultRequestDto input, CancellationToken cancellationToken = default)
        {
            PagedResultDto<DataDictionaryResultDto> pagedResultDto = await GetAll(input, cancellationToken);
            List<DataDictionaryResultDto> dataDictionaryResultDtos = GetDataDictionarys(pagedResultDto.Items, "", input);
            return dataDictionaryResultDtos;
        }

        private List<DataDictionaryResultDto> GetDataDictionarys(IReadOnlyList<DataDictionaryResultDto> dataDictionaryResultDtos, string parentCode, DataDictionaryResultRequestDto input)
        {
            List<DataDictionaryResultDto> resultDtos = new List<DataDictionaryResultDto>();
            foreach (DataDictionaryResultDto dataDictionaryResultDto in dataDictionaryResultDtos)
            {
                if (!parentCode.Equals(dataDictionaryResultDto.ParentCode))
                {
                    continue;
                }

                string code = input.Code;
                if (!code.IsNullOrWhiteSpace() && code.Equals(dataDictionaryResultDto.Code))
                {
                    continue;
                }

                dataDictionaryResultDto.Childrens = GetDataDictionarys(dataDictionaryResultDtos, dataDictionaryResultDto.Code, input);
                resultDtos.Add(dataDictionaryResultDto);
            }
            return resultDtos.Count > 0 ? resultDtos : null;
        }
        #endregion

        public override async Task<DataDictionaryResultDto> Post(DataDictionaryDto input, CancellationToken cancellationToken = default)
        {
            bool any = await MasterRepository.AnyAsync(dd => dd.Code.Equals(input.Code));
            if (any)
            {
                throw Oh("数据字典编码已存在");
            }
            DataDictionaryResultDto output = await base.Post(input, cancellationToken);
            return output;
        }

        //protected override async Task OnUpdateExecuting(DataDictionary entity, CancellationToken cancellationToken)
        //{
        //    await base.OnUpdateExecuting(entity, cancellationToken);
        //    bool any = await MasterRepository.AnyAsync(dd => dd.Id != entity.Id && dd.Code.Equals(entity.Code));
        //    if (any)
        //    {
        //        throw Oh("数据字典编码已存在");
        //    }
        //}

        protected override async Task OnDeleteExecuting(DataDictionary entity, CancellationToken cancellationToken)
        {
            await base.OnDeleteExecuting(entity, cancellationToken);
            bool any = await MasterRepository.AnyAsync(dd => dd.ParentCode.Equals(entity.Code));
            if (any)
            {
                throw Oh("该数据字典有子级，不能删除");
            }
        }
    }
}
